from flask import Blueprint, request
from service.user_message_service import UserMessageService
from utils.securtiy import login_required
from utils.tools import json_response
from utils.work_db import get_session
from utils.work_log import get_logger
from utils.securtiy import get_user_id

log = get_logger(__name__)

api = Blueprint('message_api', __name__, url_prefix='/api/v1/message')
"""
{
   "message_type":
   1:'系统消息',
   2:'简历回执'
   消息类型 1:系统消息 2:个人端-简历投递回执 3:(企业端)发布工作-审核通过
}
"""


@api.route('/get_message', methods=['POST'])
@login_required
def get_message(**kwargs):
    """

    :param kwargs:
    :return:
    """
    params = request.json
    d = dict()
    d["user_id"] = get_user_id()
    if params.get("message_type"):
        d["message_type"] = params.get("message_type")
    session = get_session()
    service = UserMessageService(session)
    msgs = service.get_messages_by_user_id(user_id=get_user_id())
    return json_response(msgs, msg='', code=1)


@api.route('/read/<msg_id>', methods=['GET'])
@login_required
def read(msg_id, **kwargs):
    """
    设置消息是否读取
    :param msg_id:
    :param kwargs:
    :return:
    """
    session = get_session()
    service = UserMessageService(session)
    msgs = service.update_read(message_id=int(msg_id))
    return json_response(msgs, msg='', code=1)
